$(function(){
	var parentHeight,parentWidth;
	parentHeight = $("#deviceType-box").height() - 93;
    parentWidth = $("#deviceType-box").width();
	$("#deviceType-table").jqGrid({
		url: contextPath + "/DeviceType/queryDeviceTypeJqg",
		datatype: "json",
		height: parentHeight,
	    width: parentWidth,
	    colNames:['','','设备类型','备注信息','操作'],
		colModel:[
			{name:"id",hidden:true},
			{name:'iconFile',hidden:true},
			{name:'name',index:'name',align:"center",width:"80px"},
			{name:'description',index:'description',align:"center"},
			{name:'moduleId',index:'moduleId',align:"center",formatter: operation,sortalbe:false,width:"50px"}
			
		],
		sortname:'id',
		sortorder:'asc',
		viewrecords: true,
		rowNum: 20,
		rowList:[5,10,20],
		pager: '#deviceType-pager'
	});
	$("#deviceType-table").jqGrid('navGrid', '#deviceType-pager', {edit : false,add : false,del : false,search: false});
	
	// 查询按钮
    $("#query").on("click",function(){
        $("#deviceType-table").setGridParam({
            url : contextPath + "/DeviceType/queryDeviceTypeJqg",
            postData : {
                queryCond:{
                    'pattern': $('#deviceTypeName').val()
                }
            },
            page: 1
        }).trigger("reloadGrid");
    });
});
function autodivheight(){
    parentHeight = $("#deviceType-box").height() - 93;
    parentWidth = $("#deviceType-box").width();
}
function operation(cellvalue,options,rowObject){
	var str = "";
	// if([[${#authorization.expression('hasAuthority(''modifyAuthority'')')}]]){
		str += "<input style='margin:5px 3px' class='btn btn-primary btn-xs modify' type='button' value='修改'>";
	// }
	// if([[${#authorization.expression('hasAuthority(''deleteAuthority'')')}]]){
		str += "<input style='margin:5px 3px' class='btn btn-danger btn-xs del' type='button' value='删除' data-toggle='modal' data-target='.bs-example-modal-sm'>";
	// }
    return str;
}
//修改
var deviceTypeData = null;
/* 修改 || 删除 */
$("#deviceType-table").on('click','.modify,.del',function(){
	var id = $(this).parent().parent().attr('id');
	deviceTypeData = $("#deviceType-table").jqGrid('getRowData',id);
    delete deviceTypeData.operate;
    if($(this).hasClass("modify")){
   		window.location.hash = "qualitycontrol-systemGroup-modifyDeviceType";
    }else{
//    	deviceTypeData.id = Number(deviceTypeData.id);
    	    }
    console.log(deviceTypeData);
});
$("#deleteData").on('click',function(){
	$.ajax({
        url:contextPath + '/DeviceType/deleteDeviceType',
        type:"delete",
        contentType:"application/json",
        dataType:"json",
        data: JSON.stringify(deviceTypeData),
        success:function(data){
            if(data.code == 1){
                $('#deleteNotice').modal('hide');
                toastr.success('删除成功！');
                $("#deviceType-table").setGridParam({
                    url : contextPath + "/DeviceType/queryDeviceTypeJqg",
                    postData : {
                        queryCond:{
                            'pattern': $('#deviceTypeName').val()
                        }
                    },
                    page: 1
                }).trigger("reloadGrid");
                deviceTypeData = null;
            }else{
                toastr.error(data.message,'删除失败！');
            }
        },
        error:function(data){
        	console.log(data);
            toastr.error('操作失败，请稍后再试！');
        }
    });
});
$(window).resize(function(){
	$("#deviceType-table").setGridWidth($('#deviceType-box').width());
	$("#deviceType-table").setGridHeight($('#deviceType-box').height() - 93);
});　
//侧边栏缩小时，实时的给jqgrid的宽和高赋值
$("#asidebarSwitch").on('click',function() {
    var time = 0;
    var set = setInterval(function (){
        $("#deviceType-table").setGridWidth($('#deviceType-box').width());　　
        $("#deviceType-table").setGridHeight($('#deviceType-box').height() - 83);
        time++;
        if(time >= 200){
            clearInterval(set);
        }
    },time);
});　
